package com.example.user_module;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.example.common.databus.LiveDataBus;
import com.example.common.mvvm.BaseEntity;
import com.example.common.mvvm.viewmodel.BaseViewModel;
import com.example.common.providerservice.CheckLoginApi;
import com.example.user_module.dao.UserEntity;
import com.example.user_module.login.UserInfoEntity;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class UserViewModel extends BaseViewModel<UserRepository> {
    private CheckLoginApi checkLoginApi;
    LiveDataBus<Map<String,Object>>liveDataBus=new LiveDataBus<>();
    public String nickName;
//    1654522678
    public String figureurl;
    @Override
    public void onResult(BaseEntity entity) {
        LogUtils.e("entity"+entity.getClass().getSimpleName());
        if (entity instanceof UserEntity){
            UserEntity userEntity= (UserEntity) entity;
            LogUtils.e("usermessage"+userEntity.message);
//            Gson gson = new Gson();
//            String s = gson.toJson(userEntity);
//            if (userEntity.message.equals("用户未登录")){
//                ARouter.getInstance().build("").navigation();
//            }else {
//                nickName=userEntity.name;
//                System.out.println("========kk"+nickName);
//            }
        }
        if (entity instanceof UserInfoEntity){
            UserInfoEntity userInfoEntity= (UserInfoEntity) entity;
            UserInfoEntity.DataBean.UserInfo data = userInfoEntity.data.data;
             nickName = data.name;
             figureurl = data.avatar;
             Map<String,Object>map=new HashMap<>();
             map.put("nickName",nickName);
             map.put("figureurl",figureurl);
             liveDataBus.postValue(map);
        }
    }
    public void login(){
        if (!checkLoginApi.checkLogin()){
            ARouter.getInstance().build("/user/LoginActivity").navigation();
        }
    }
    @Override
    protected void initData() {
         checkLoginApi = (CheckLoginApi) ARouter.getInstance().build("/user/CheckLoginImpl").navigation();
        boolean b = checkLoginApi.checkLogin();
        long userId = checkLoginApi.geUserId();
         if (b){
             repository.requestQueryUser(userId)
                     .subscribeOn(Schedulers.io())
                     .observeOn(AndroidSchedulers.mainThread())
                     .subscribe(this);
//             repository.queryUser()
//                     .subscribeOn(Schedulers.io())
//                     .observeOn(AndroidSchedulers.mainThread())
//                     .subscribe(this);
         }else {
             ARouter.getInstance().build("/user/LoginActivity").navigation();
         }

    }
    public void refreshUser(){
        long userId = checkLoginApi.geUserId();
        repository.requestQueryUser(userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
//        repository.queryUser()
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(this);

    }
}
